package com.vmj.natives.sun.reflect;

import com.vmj.heap.VMClass;
import com.vmj.heap.VMObject;
import com.vmj.natives.NativeMethod;
import com.vmj.natives.Registry;
import com.vmj.rtda.Frame;
import com.vmj.rtda.LocalVars;
import com.vmj.rtda.OperandStack;

/**
 * Created by yangshoujun on 2017/9/17 下午2:34.
 */
public class Reflaction {

    public static final java.lang.String jlClass = "sun/reflect/Reflection";

    public static void init() {
        Registry.register(jlClass, "getCallerClass", "()Ljava/lang/Class;", new NativeMethod() {
            public void execute(Frame frame) {
                Frame callerFrame = frame.getThread().getFrames()[2];
                VMObject callerClass = callerFrame.getMethod().getClazz().getjClass();
                frame.getOperandStack().pushRef(callerClass);
            }
        });

        Registry.register(jlClass, "getClassAccessFlags", "(Ljava/lang/Class;)I", new NativeMethod() {
            public void execute(Frame frame) {
                LocalVars vars = frame.getLocalVars();
                VMObject _type = vars.getRef(0);

                VMClass clazz = (VMClass) _type.getExtra();
                int flags = clazz.getAccessFlags();

                OperandStack stack = frame.getOperandStack();
                stack.pushInt(flags);
            }
        });
    }
}
